package com.example.bot_cc.broadnotifytest_420clj;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

/**
 * Created by BOT-CC on 2017/5/4.
 */

public class MyReceiver extends BroadcastReceiver {
    private Context context;//上下文
    private String str;
    public static NotificationManager manager;
    public static int notifyId = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        str = intent.getStringExtra("para1");
        Toast.makeText(context, "接收到广播："+str,Toast.LENGTH_SHORT).show();
        sendnotify();//发送通知
    }
    private void sendnotify(){
        //1获取manager
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //2实例化Builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        //3配置Builder
        //点击通知后打开新窗口
        Intent intent = new Intent(context, NotifyActivity.class);
        intent.putExtra("para2", str);
        //配置默认必选的属性
        PendingIntent pendingIntent = PendingIntent.getActivity(context,//上下文
                                                                notifyId,//进入新窗口的通知id
                                                                intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("通知的标题")
                .setContentText("通知的内容" + str)
                .setContentIntent(pendingIntent)
                .setTicker("通知来了")
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis());
        //4发送通知
        manager.notify(notifyId,builder.build());
    }
}
